package com.yuhs.zixing.study.springboot.redis.controller;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @description: redis 测试控制器
 * @author: yuhs
 * @date: 2022/5/8
 */
@Slf4j
@RestController
@RequestMapping("/test/redis")
public class TestRedisController {


    @Resource
    private RedisTemplate<Object, String> redisTemplate;

    @Resource
    private RedissonClient redissonClient;


    @SneakyThrows
    @RequestMapping("/String")
    public String testRedisString(String key) {


        RLock lock = redissonClient.getLock("keykeykey");
        try {
            boolean b = lock.tryLock(10, TimeUnit.SECONDS);
            log.info("redisson 获取锁 结果 {}", b);

            //执行业务
            Thread.sleep(6000L);

        } catch (Exception e) {

        }
        ValueOperations<Object, String> stringValueOperations = redisTemplate.opsForValue();

        //操作字符串
        stringValueOperations.set(key, key + "|" + key, 10, TimeUnit.SECONDS);
        String s = stringValueOperations.get(key);

        log.info("从redis 中获取的value {}", s);
//        Thread.sleep(60 * 1000L);

        return s;
    }

}
